package com.puge.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.puge.eduservice.entity.EduSubject;
import com.puge.eduservice.entity.excel.SubjectData;
import com.puge.eduservice.entity.subject.OneSubject;
import com.puge.eduservice.entity.subject.TwoSubject;
import com.puge.eduservice.listener.SubjectExcelListener;
import com.puge.eduservice.mapper.EduSubjectMapper;
import com.puge.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-13
 */

@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

//    添加课程分类
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try{
//            1,获取文件输入流
            InputStream in = file.getInputStream();
//             2,调用方法进行读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

//    课程分类列表()
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
//        1,1查询所有的一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0"); //判断数据库中parent_id字段的信息为0的数据

        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);


//        1,2查询所有的二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");  //判断数据库中parent_id字段的信息不为0的数据

        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

//        2,1创建list集合，用于存储最终的封装结果
        List<OneSubject> finalSubjectList = new ArrayList<>();

//        3,1封装查询出来的一级分类
/**
 *      查询出来所有的一级分类list集合遍历。得到每个一级分类对象，获取每个一级分类对象的值
 *      封装到要求的list集合里面：     oneSubjectList————》finalSubjectList     * */
        for (int i = 0; i < oneSubjectList.size(); i++) {
//          把eduSubject里面的值获取出来，放到OneSubject的里面
            EduSubject eduSubject = oneSubjectList.get(i);
//          把多个OneSubject放到finalSubject里面
            OneSubject oneSubject = new OneSubject();

/**            oneSubject.setId(eduSubject.getId());  //获取id并存入oneSubject内
            oneSubject.setTitle(eduSubject.getTitle());  //获取title并存入oneSubject内*/
            //将eduSubject中的值复制到oneSubject集合中去，代替了上面的逐个遍历，简化了代码，但在底层实际也是用了上面遍历的方法
            BeanUtils.copyProperties(eduSubject,oneSubject);

            // 将得到的id和title存入最终集合内
            finalSubjectList.add(oneSubject);


//            在以及分类中循环遍历查询所有的二级分类
//            创建list集合封装每个一级分类的二级分类
            List<TwoSubject> twoFinalSubjects = new ArrayList<>();
//            遍历二级分类list集合
            for (int j = 0; j <twoSubjectList.size() ; j++) {
//             获取每个二级分类
                EduSubject tSubject = twoSubjectList.get(j);
//                判断二级分类的parent_id和一级分类的id值是否一样
                if (tSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    //将每一个遍历输出的二级分类的信息存入到  twoFinalSubjects 集合中去
                    twoFinalSubjects.add(twoSubject);

                }
            }
            //将所有查询出来的二级分类的信息存入一级分类内
            oneSubject.setChildren(twoFinalSubjects);
        }
//      返回最终存储数据的集合
        return finalSubjectList;
    }
}
